//
//  HQualitySave.m
//  NursingManages
//
//  Created by HuangZhen on 2017/6/7.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HQualitySave.h"
#import "DBHelper.h"
@implementation HQualitySave

-(instancetype)initWithFormData:(Model_Hr_Ns_Inspect_New *)inspectModel inspectDetailArray:(NSArray *)inspectDetailArray{
    
    NSLog(@"=====%@",inspectModel.CHECK_DATE);
    NSString *string =   [self GetInspectSql:inspectModel];
    NSArray *array =   [self GetInspectDetailSql:inspectDetailArray];

    @try {
        
        [DBHelper.DB open];
        [DBHelper.DB beginTransaction];
        
        [DBHelper.DB executeUpdate:string];
        
        for (NSString  * sql  in array) {
            [DBHelper.DB executeUpdate:sql];
        }
        
        [DBHelper.DB commit];
        
        
    } @catch (NSException *exception) {
        
        [DBHelper.DB rollback];
        NSLog(@"发现错误，数据已回滚：%@",exception);
        
    } @finally {
        [DBHelper.DB close];
    }

    
    return self;
}

-(instancetype)initWithFormDatainspectDetailArray:(NSArray*) inspectDetailArray
{
    NSArray *array =   [self GetInspectDetailSql:inspectDetailArray];
    
    @try {
        
        [DBHelper.DB open];
        [DBHelper.DB beginTransaction];
        
        
        for (NSString  * sql  in array) {
            [DBHelper.DB executeUpdate:sql];
        }
        
        [DBHelper.DB commit];
        
        
    } @catch (NSException *exception) {
        
        [DBHelper.DB rollback];
        NSLog(@"发现错误，数据已回滚：%@",exception);
        
    } @finally {
        [DBHelper.DB close];
    }
    
    
    return self;

}
-(instancetype)initWithFormData:(Model_Hr_Ns_Inspect_New *)inspectModel{
    
        NSString *string =   [self GetInspectSql:inspectModel];
    
    @try {
        
        [DBHelper.DB open];
        [DBHelper.DB beginTransaction];
        
        [DBHelper.DB executeUpdate:string];
        
                
        [DBHelper.DB commit];
        
        
    } @catch (NSException *exception) {
        
        [DBHelper.DB rollback];
        NSLog(@"发现错误，数据已回滚：%@",exception);
        
    } @finally {
        [DBHelper.DB close];
    }
    
    
    return self;
}


-(instancetype)initWithUpdateFormDataArray:(NSArray *)inspectDetailArray
{
    NSArray *array =   [self upDateInspecDetail_new:inspectDetailArray];
    @try {
        
        [DBHelper.DB open];
        [DBHelper.DB beginTransaction];
                
        for (NSString  * sql  in array) {
            [DBHelper.DB executeUpdate:sql];
        }
        
        [DBHelper.DB commit];
        
        
    } @catch (NSException *exception) {
        
        [DBHelper.DB rollback];
        NSLog(@"发现错误，数据已回滚：%@",exception);
        
    } @finally {
        [DBHelper.DB close];
    }
    
    
    return self;
    

}

-(instancetype)initWithUpdateFormData:(Model_Hr_Ns_Inspect_New *)inspectModel
                   inspectDetailArray:(NSArray *)inspectDetailArray
{
    
    NSString *string =   [self upDateInspec_new:inspectModel];
    NSArray *array =   [self upDateInspecDetail_new:inspectDetailArray];
    
    
    @try {
        
        [DBHelper.DB open];
        [DBHelper.DB beginTransaction];
        
        [DBHelper.DB executeUpdate:string];
        
        for (NSString  * sql  in array) {
            [DBHelper.DB executeUpdate:sql];
        }
        
        [DBHelper.DB commit];
        
        
    } @catch (NSException *exception) {
        
        [DBHelper.DB rollback];
        NSLog(@"发现错误，数据已回滚：%@",exception);
        
    } @finally {
        [DBHelper.DB close];
    }
    
    
    return self;

}

- (NSString *)upDateInspec_new:(Model_Hr_Ns_Inspect_New *)model
{
    
    NSString *sql = [NSString stringWithFormat:@"update hr_ns_inspect_new set level_id = '%@' , code_id = '%@' , type_id = '%@', standard_id = '%@', dept_id = '%@', check_date = '%@', checker_id = '%@', checker_name = '%@', score = '%.2f', resul = '%@', remarks = '%@', input_user_id = '%@', input_user_name = '%@', input_date = '%@', othher_chaeck_id = '%@', upload_flag = '%ld', othher_chaeck_name = '%@', check_status = '%ld', issubmit = '%ld', delere_flag = '%ld',nurse_id = '%@', nurse_name = '%@',round_hours = '%@' where result_id = '%@'",model.LEVEL_ID,model.CODE_ID,model.TYPE_ID,model.STANDARD_ID,model.DEPT_ID,model.CHECK_DATE,model.CHECKER_ID,model.CHECKER_NAME,[model.SCORE floatValue],model.RESUL,model.REMARKS,model.INPUT_USER_ID,model.INPUT_USER_NAME,model.INPUT_DATE,model.OTHHER_CHAECK_ID,(long)model.UPLOAD_FLAG,model.OTHHER_CHAECK_NAME,model.CHECK_STATUS,model.ISSUBMIT,model.DELERE_FLAG,model.NURSE_ID?:@"",model.NURSE_NAME?:@"",model.ROUND_HOURS,model.RESULT_ID];
    return sql;
}

- (NSMutableArray  *)upDateInspecDetail_new:(NSArray  *)array
{
    NSMutableArray *arrSql = [[NSMutableArray alloc]init];
    for (Model_Hr_InspectDetail_New *model in array) {
        NSString *sql = [NSString stringWithFormat:@"update hr_ns_inspectdetail_new set  score = '%.2f' , resion = '%@', ingore_flag = '%ld', one_flag = '%ld', prize = '%f', penalty = '%f', resion_id = '%@', upload_flag = '%ld',frequency = '%ld' where result_id = '%@' AND code_id = '%@'",model.SCORE,model.RESION,model.INGORE_FLAG,model.ONE_FLAG,model.PRIZE,model.PENALTY,model.RESION_ID,model.UPLOAD_FLAG,model.FREQUENCY,model.RESULT_ID,model.CODE_ID];
        [arrSql addObject:sql];
        NSLog(@"%@  %@",model.CODE_ID ,model.RESULT_ID);
    }

    return arrSql;

}



-(NSString*) GetInspectSql:(Model_Hr_Ns_Inspect_New*)model{
    
    NSString *sql1 = @"insert into hr_ns_inspect_new(";
    NSString *sql2 = @" values(";
    
    sql1 = [sql1 stringByAppendingString:@"result_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.RESULT_ID]];
    
    sql1 = [sql1 stringByAppendingString:@"level_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.LEVEL_ID]];
    
    sql1 = [sql1 stringByAppendingString:@"code_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.CODE_ID]];

    sql1 = [sql1 stringByAppendingString:@"type_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.TYPE_ID]];
    
    sql1 = [sql1 stringByAppendingString:@"standard_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.STANDARD_ID]];
    
    sql1 = [sql1 stringByAppendingString:@"dept_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.DEPT_ID]];
    
    sql1 = [sql1 stringByAppendingString:@"check_date,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.CHECK_DATE]];
    
    sql1 = [sql1 stringByAppendingString:@"round_hours,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.ROUND_HOURS]];
    
    sql1 = [sql1 stringByAppendingString:@"nurse_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.NURSE_ID]];

    sql1 = [sql1 stringByAppendingString:@"nurse_name,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.NURSE_NAME]];
    
    
    sql1 = [sql1 stringByAppendingString:@"checker_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.CHECKER_ID]];
    
    sql1 = [sql1 stringByAppendingString:@"checker_name,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.CHECKER_NAME]];
    
    sql1 = [sql1 stringByAppendingString:@"score,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%.2f',",[model.SCORE floatValue]]];
    
    sql1 = [sql1 stringByAppendingString:@"resul,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.RESUL]];
    
    sql1 = [sql1 stringByAppendingString:@"resion_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.RESION_ID]];
    

    sql1 = [sql1 stringByAppendingString:@"remarks,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.REMARKS]];
    
    sql1 = [sql1 stringByAppendingString:@"input_user_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.INPUT_USER_ID]];

    sql1 = [sql1 stringByAppendingString:@"input_user_name,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.INPUT_USER_NAME]];
    
    sql1 = [sql1 stringByAppendingString:@"input_date,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.INPUT_DATE]];
    
    sql1 = [sql1 stringByAppendingString:@"othher_chaeck_id,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.OTHHER_CHAECK_ID?:@""]];
    
    sql1 = [sql1 stringByAppendingString:@"upload_flag,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%ld',",(long)model.UPLOAD_FLAG]];

    
    sql1 = [sql1 stringByAppendingString:@"othher_chaeck_name,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.OTHHER_CHAECK_NAME?:@""]];
    

    sql1 = [sql1 stringByAppendingString:@"check_status,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@0]];
    
    sql1 = [sql1 stringByAppendingString:@"issubmit,"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@0]];
    
    sql1 = [sql1 stringByAppendingString:@"delere_flag"];
    sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@'",@0]];
    

    sql1 = [sql1 stringByAppendingString:@")"];
    sql2 = [sql2 stringByAppendingString:@")"];
    
    NSString *sql3 = [sql1 stringByAppendingString:sql2];
    
    NSLog(@"%@",sql3);
    return sql3;
}

-(NSArray*) GetInspectDetailSql:(NSArray*)arr{
    
    NSMutableArray *arrSql = [[NSMutableArray alloc]init];
    for (Model_Hr_InspectDetail_New* model in arr) {
        NSString *sql1 = @"insert into hr_ns_inspectdetail_new(";
        NSString *sql2 = @" values(";
        
        sql1 = [sql1 stringByAppendingString:@"result_id,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.RESULT_ID]];
        
        sql1 = [sql1 stringByAppendingString:@"code_id,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.CODE_ID]];
        
        sql1 = [sql1 stringByAppendingString:@"score,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%f',",model.SCORE]];
        
        sql1 = [sql1 stringByAppendingString:@"resion,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.RESION]];
        
        sql1 = [sql1 stringByAppendingString:@"ingore_flag,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@0]];
        
        sql1 = [sql1 stringByAppendingString:@"one_flag,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@0]];
        
        sql1 = [sql1 stringByAppendingString:@"prize,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@0]];
        
        sql1 = [sql1 stringByAppendingString:@"penalty,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@1]];
        
        sql1 = [sql1 stringByAppendingString:@"resion_id,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.RESION_ID]];
        
        sql1 = [sql1 stringByAppendingString:@"frequency,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%ld',",(long)model.FREQUENCY]];

        sql1 = [sql1 stringByAppendingString:@"upload_flag,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",@0]];

        sql1 = [sql1 stringByAppendingString:@"par_id,"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%@',",model.par_id]];
        
        sql1 = [sql1 stringByAppendingString:@"check_flag"];
        sql2 = [sql2 stringByAppendingString:[NSString stringWithFormat:@"'%ld'",(long)model.check_flag]];
        

        
        
        sql1 = [sql1 stringByAppendingString:@")"];
        sql2 = [sql2 stringByAppendingString:@")"];
        NSString *sql3 = [sql1 stringByAppendingString:sql2];
        NSLog(@"detail%@",sql3);

        [arrSql addObject:sql3];
    }
    
    return arrSql;
}

@end
